using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.MediaCenter.UI;
using Microsoft.MediaCenter.Hosting;
using System.Collections;
using Level65.ClipShow;
using Level65.WMVHelper;

namespace ClipShow
{
    public class ChannelBuilderCodeBehind : CoreUICodeBehind
    {
        private Choice _myChosenContentItems;
        private bool _firstItemIn = true;
        private bool _showVideoSources = true;
        private string _parentTitle = "";

        public ChannelBuilderCodeBehind()
        {
            base.BuildLibraryOptions(false, true);
            _myChosenContentItems = new Choice();
            BuildMediaSelector(0);
            BuildCommandBar();
        }

        public ChannelBuilderCodeBehind(MediaMetaData parent)
        {
            base.BuildLibraryOptions(false, true);
            _myChosenContentItems = new Choice();
            if(!BuildVideoChildren(parent)) 
            {
                // We found no linked videos, so place itself in the list to prevent screen craah
                ArrayListDataSet temp = new ArrayListDataSet();
                temp.Add(parent);
                base.MediaLibraryItems.Options = temp;
            }

            try
            {
                BuildCommandBar();
                _showVideoSources = false;
                _firstItemIn = false;
                ArrayListDataSet temp = new ArrayListDataSet();
                temp.Add(parent);
                _myChosenContentItems.Options = temp;
                _parentTitle = String.Format("Linked media to '{0}'", parent.Title);
            }
            catch (SystemException se)
            {
                base.ShowDialog("clipShow Exception", se.Message, true);
            }
        }

        #region Private helper routines
        private void BuildCommandBar()
        {
            ArrayListDataSet tempCommandBarItems = new ArrayListDataSet();
            tempCommandBarItems.Add(new CommandBarItem("Play", "play selected clips"));
            tempCommandBarItems.Add(new CommandBarItem("Browse", "browse my media"));
            base.AddCommandBarItems(tempCommandBarItems);
        }

        private bool BuildVideoChildren(MediaMetaData sourceItem)
        {
            Topics tp = new Topics();
            string firstTerm = tp.BuildSingleTopic(sourceItem);

            ArrayListDataSet sourceVideos = base.PopulateMediaLibrary();
            ArrayListDataSet temp = new ArrayListDataSet();

            for (int Ctr = 0; Ctr < sourceVideos.Count; Ctr++)
            {
                MediaMetaData source = (MediaMetaData)sourceVideos[Ctr];
                string[] terms = firstTerm.Split(';');
                foreach (string term in terms)
                {
                    if (base.CheckForKeyword(source, term))
                    {
                        temp.Add(source);
                        break;
                    }
                }
            }
            if (temp.Count > 0)
            {
                base.MediaLibraryItems.Options = temp;
                return (true);
            }
            else
            {
                return (false);
            }
        }

       #endregion

        #region Properties
        public string ParentTitle
        {
            get
            {
                return (_parentTitle);
            }
        }

        public Choice ChosenMediaItems
        {
            get { return (_myChosenContentItems); }
        }
        public bool ShowParentTitle
        {
            get
            {
                return (!_showVideoSources);
            }
        }
        public bool ShowVideoSourcesMenu
        {
            get
            {
                return (_showVideoSources);
            }
        }

        public bool VideosAdded
        {
            get
            {
                return (!_firstItemIn);
            }
        }
        public ChannelTextBox ResultPageToSave
        {
            get
            {
                ClipCollection mc = new ClipCollection();
                mc.ClipLength = MediaClipLengths.Long;
                mc.Name = "Clip Name";

                ClipItem ci = new ClipItem();
                ci.Keywords = "ALL";
                ci.Name = "my playlists";

                MediaMetaData tempMd = (MediaMetaData)this._myChosenContentItems.Options[0];
                ci.ThumbnailUrl = tempMd.ThumbnailUrl;
                ci.Count = this._myChosenContentItems.Options.Count;

                foreach (MediaMetaData md in this._myChosenContentItems.Options)
                {
                    ci.MediaItems.Add(md);
                }

                mc.Items.Add(ci);

                ChannelTextBox result = new ChannelTextBox();
                result.SourceChannel = mc;
                result.ChannelItems = this._myChosenContentItems;

                
                return (result);

            }
        }
        public MainCodeBehind ResultPageWithChannel
        {
            get
            {
                ClipCollection mc = new ClipCollection();
                mc.ClipLength = base.AppSettings.DefaultClipLength;
                mc.Name = "Custom Channel";

                ClipItem ci = new ClipItem();
                ci.Keywords = "ALL";
                ci.Name = "My Channel";

                Random r = new Random(unchecked((int)DateTime.Now.Ticks));
                int mediaItem = r.Next(0, this._myChosenContentItems.Options.Count);

                MediaMetaData tempMd = (MediaMetaData)this._myChosenContentItems.Options[mediaItem];
                ci.ThumbnailUrl = tempMd.ThumbnailUrl;
                ci.Count = this._myChosenContentItems.Options.Count;

                foreach (MediaMetaData md in this._myChosenContentItems.Options)
                {
                    ci.MediaItems.Add(md);
                }

                mc.Items.Add(ci);

                MainCodeBehind cb = new MainCodeBehind();
                cb.CreateChannel(mc, this._myChosenContentItems);

                ArrayListDataSet temp = new ArrayListDataSet();
                temp.Add(mc);
                cb.ToplineChoices.Options = temp;
                cb.CurrentChannel = mc;

                cb.PlayClip();

                return (cb);
            }
        }
        #endregion

        #region Methods
        public void FillPlaceholder(Image placeHolder)
        {
            if (_myChosenContentItems.Options == null)
            {
                // Create place holder for selection items
                ArrayListDataSet temp = new ArrayListDataSet();
                MediaMetaData md = new MediaMetaData();
                md.Thumbnail = placeHolder;
                temp.Add(md);
                _myChosenContentItems.Options = temp;
            }
        }

        public void AddMediaItem(int mediaIndex)
        {
            MediaMetaData med = (MediaMetaData)base.MediaLibraryItems.Options[mediaIndex];
            _myChosenContentItems.Options.Add(med);
            if (_firstItemIn)
            {
                _firstItemIn = false;
                _myChosenContentItems.Options.RemoveAt(0);
            }
        }

        public void RemoveMediaItem(int mediaIndex)
        {
            if (!_firstItemIn)
            {
                _myChosenContentItems.Options.RemoveAt(mediaIndex);
            }
        }
        #endregion
    }
}
